<?php
/**
 * Util class
 *
 * This file contains a bunch of utility functions used for everything from
 * MySQL queries, to posting messages to the Layout class via SESSION variables.
 * @author Michael Thomas <michael.thomas@gmail.com>
 * @version 5.25.2008
 * @package csforms
 */

class Util
{
  const VAR_SESSION_MESSAGE = 'session_message';

  public static function forceHTTPS()
  {
    if (!isset($_SERVER['HTTPS']) || ($_SERVER['HTTPS'] !== 'on')) {
      $location = "https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
      header("Location: $location");
    }
  }

  public static function showAllErrors()
  {
    error_reporting(E_ALL);
    ini_set('display_errors', true);
  }

  public static function logAllErrors()
  {
    error_reporting(E_ALL);
    ini_set('display_errors', false);
  }

  /*
    Perform a MySQL query and return the result.
  */
  public static function query($query)
  {
    $result = mysql_query($query) or Util::queryFail($query, mysql_error());
    return $result;
  }

  public static function queryFail($query, $mysqlError)
  {
    echo "'$query'\n";
    echo "$mysqlError\n";
    throw new LogicException('The MySQL query failed');
  }

  /*
    Perform a MySQL query and return the complete result set in a 2 dimensional
    array.
  */
  public static function queryFetchAll($query, $option = MYSQL_BOTH)
  {
    $result = Util::query($query);
    $resultArray = array();
    while ($data = mysql_fetch_array($result, $option)) {
      $resultArray[] = $data;
    }
    return $resultArray;
  }

  /*
    Perform a MySQL query and return the first column of each result row in an
    array. Optimally, you'd only be fetching a single column, since that's all
    you'll get back.
  */
  public static function queryFetchColumn($query)
  {
    $result = Util::query($query);
    $resultArray = array();
    while ($data = mysql_fetch_row($result)) {
      $resultArray[] = $data[0];
    }
    return $resultArray;
  }

  /*
    Perform a MySQL query and return the first row.
  */
  public static function queryFetchRow($query, $option = MYSQL_BOTH)
  {
    $result = Util::query($query);
    return mysql_fetch_array($result, $option);
  }

  /*
    Fetches a single random row from a mysql table. Note that this method is
    ridiculously faster than using 'ORDER BY RAND()'.
  */
  public static function queryFetchRandomRow($table, $option = MYSQL_BOTH,
      $database = null, $constraint = '')
  {
    $from = ($database == null) ? $table : "$database.$table";
    $count = Util::queryFetchValue("SELECT COUNT(*)
                                    FROM $from
                                    $constraint");
    $offset = mt_rand(0, intval($count) - 1);
    return Util::queryFetchRow("SELECT *
                                FROM $from
                                $constraint
                                LIMIT $offset, 1");
  }

  /*
    Perform a MySQL query and return the first item in the first row.
  */
  public static function queryFetchValue($query)
  {
    $result = Util::query($query);
    $data = mysql_fetch_row($result);
    return $data[0];
  }

  public static function getMysqlSafeString($string)
  {
    if (get_magic_quotes_gpc() != 1) {
      $string = stripslashes($string);
    }
    return mysql_real_escape_string($string);
  }

  public static function getCurrentUrl()
  {
    $http = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ?
        'https://' : 'http://';
    $url = $http . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    return $url;
  }

  public static function setSessionMessage($message)
  {
    $_SESSION[self::VAR_SESSION_MESSAGE] = $message;
  }

  public static function unsetSessionMessage()
  {
    unset($_SESSION[self::VAR_SESSION_MESSAGE]);
  }

  public static function getSessionMessage()
  {
    if (isset($_SESSION[self::VAR_SESSION_MESSAGE])) {
      return $_SESSION[self::VAR_SESSION_MESSAGE];
    }
  }
}

?>